// 信息配置，每次刷新页面会刷新一次
import { watch } from 'vue'
import { defineStore } from 'pinia'
import { zhConfigInfo } from '../lang/zh/configInfo'
import { enConfigInfo } from '../lang/en/configInfo'
import { idConfigInfo } from '../lang/id/configInfo'
import { jpConfigInfo } from '../lang/jp/configInfo'
import { krConfigInfo } from '../lang/kr/configInfo'
import { myConfigInfo } from '../lang/my/configInfo'
import { tcConfigInfo } from '../lang/tc/configInfo'
import { thConfigInfo } from '../lang/th/configInfo'
import { vnConfigInfo } from '../lang/vn/configInfo'
import { GetCategory } from '@/api/bussiness'
import { getAllModule } from '@/api/member'
import { getCountries } from '@/api/common'
import Cookies from 'js-cookie'

// 第一个参数是模块名称，唯一标识该模块，第二个参数为一个包含state、getters、actions属性的对象
export const useConfigStore = defineStore('config', {
  persist: true,
  state() {
    // 定义状态属性的方法，返回值为定义的状态属性对象
    return {
      talentChain: [],
      countryList: [],
      chainTree: [],
      moduleList: [],
    }
  },
  getters: {
    // 相当于组件中的计算属性，可以缓存数据
    getTalentChain() {
      return this.talentChain
    },
    getCountryList() {
      return this.countryList
    },
    getChainTree() {
      return this.chainTree
    },
    getXLCode() {
      try {
        const obj = this.talentChain.find((item) => item.code === 'XL')
        return obj.list
      } catch (error) {
        ElMessageBox.confirm(this.talentChain, {
          confirmButtonText: '好的',
          center: true,
          showCancelButton: false,
        })
        return null
      }
    },
    getGZJYCode() {
      const obj = this.talentChain.find((item) => item.code === 'GZJY')
      return obj.list
    },
    getSSCode() {
      const obj = this.talentChain.find((item) => item.code === 'SS')
      return obj.list
    },
    getFLDYCode() {
      const obj = this.talentChain.find((item) => item.code === 'FLDY')
      return obj.list
    },
    getQZCode() {
      const obj = this.talentChain.find((item) => item.code === 'QZ')
      return obj.list
    },
    getDGSJCode() {
      const obj = this.talentChain.find((item) => item.code === 'DGSJ')
      return obj.list
    },
    getXZLXCode() {
      const obj = this.talentChain.find((item) => item.code === 'XZLX')
      return obj.list
    },
    getGYSXCDCode() {
      const obj = this.talentChain.find((item) => item.code === 'GYSXCD')
      return obj.list
    },
    getQZZZCode() {
      const obj = this.talentChain.find((item) => item.code === 'QZZZ')
      return obj.list
    },
    getSFQZCode() {
      const obj = this.talentChain.find((item) => item.code === 'SFQZ')
      return obj.list
    },
    getJYMSCode() {
      const obj = this.talentChain.find((item) => item.code === 'JYMS')
      return obj.list
    },
    getVIPCode() {
      const obj = this.talentChain.find((item) => item.code === 'VIP')
      return obj.list
    },
    getQYJYXSCode() {
      const obj = this.talentChain.find((item) => item.code === 'QYJYXS')
      return obj.list
    },
    getJOB_CKJJCode() {
      const obj = this.talentChain.find((item) => item.code === 'JOB_CKJJ')
      return obj.list
    },
    getJOB_BJJJCode() {
      const obj = this.talentChain.find((item) => item.code === 'JOB_BJJJ')
      return obj.list
    },
    getHBDWCode() {
      console.log(this.talentChain);
      
      const obj = this.talentChain.find((item) => item.code === 'HBDW')
      return obj.list
    },
    getGZQZCode() {
      const obj = this.talentChain.find((item) => item.code === 'GZQZ')
      return obj.list
    },
    getYQSSCode() {
      const obj = this.talentChain.find((item) => item.code === 'YQSS')
      return obj.list
    },
    getXZJGCode() {
      const obj = this.talentChain.find((item) => item.code === 'XZJG')
      return obj.list
    },
    getXBCode() {
      const obj = this.talentChain.find((item) => item.code === 'XB')
      return obj.list
    },
  },
  actions: {
    initTalentChain() {
      const myLang = Cookies.get('language') ? Cookies.get('language') : 'zh'
      switch (myLang) {
        case 'zh':
          this.talentChain = zhConfigInfo
          break
        case 'en':
          this.talentChain = enConfigInfo
          break
        case 'id':
          this.talentChain = idConfigInfo
          break
        case 'jp':
          this.talentChain = jpConfigInfo
          break
        case 'kr':
          this.talentChain = krConfigInfo
          break
        case 'tc':
          this.talentChain = tcConfigInfo
          break
        case 'vn':
          this.talentChain = vnConfigInfo
          break
        case 'th':
          this.talentChain = thConfigInfo
          break
        case 'my':
          this.talentChain = myConfigInfo
          break
        default:
          this.talentChain = zhConfigInfo
          break
      }
    },
    initCountryList() {
      // 尝试从本地存储获取数据
      if (this.countryList.length > 0) {
        return Promise.resolve(this.countryList)
      }
      return this.refleshCountryList()
    },
    refleshCountryList() {
      // 如果没有找到或数据无效，则从远程获取
      getCountries()
        .then((res) => {
          if (res.code === 20000) {
            this.countryList = res.data
            return res.data
          } else {
            return []
          }
        })
        .catch((error) => {
          return []
        })
    },
    initChainTree() {
      // 尝试从本地存储获取数据
      if (this.chainTree.length > 0) {
        return Promise.resolve(this.chainTree)
      }
      return this.refleshChainTree()
    },
    refleshChainTree() {
      // 如果没有找到或数据无效，则从远程获取
      return GetCategory(0)
        .then((res) => {
          if (res.code === 20000) {
            console.log(res.data)
            this.chainTree = res.data
            return res.data
          } else {
            return []
          }
        })
        .catch((error) => {
          return []
        })
    },
    watchChainTree() {
      watch(
        () => this.chainTree,
        (newVal) => {
          if (Array.isArray(newVal) && newVal.length === 0) {
            this.initChainTree()
          }
        },
        { immediate: true },
      )
    },
    initModuleList() {
      // 尝试从本地存储获取数据
      if (this.moduleList.length > 0) {
        return Promise.resolve(this.moduleList)
      }
      return this.refleshModuleList()
    },
    refleshModuleList() {
      // 如果没有找到或数据无效，则从远程获取
      return getAllModule()
        .then((res) => {
          if (res.code === 20000) {
            console.log(res.data)
            this.moduleList = res.data
            return res.data
          } else {
            return []
          }
        })
        .catch((error) => {
          return []
        })
    },
  },
})
